﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using System.Web;
using System.Threading.Tasks;
using System.Data;
using BartonExtension;
using ResponseObject;
using SQLWrapper;
using Model.App;
using Model.Images;
using Model.Weixin;
using Services.Common;
using Services.User;

namespace iShangMai.Controllers
{
    public class PeopleController : ApiController
    {
        [HttpGet]
        public async Task<ZKResponse> GetFriendList(string token, Int64 uid)
        {
            ZKResponse response = new ZKResponse();
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("token", token);
            param.Add("uid", uid);

            ISMUserServiceProvider provider = (ISMUserServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.USER];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMUserServiceProvider.USER_GET_FRIEND_LIST,
                userinfo = param
            };
            var friends_result = await provider.doProcess(model);
            if (friends_result.isOperationSuccess)
            {
                Dictionary<string, object> finalResult = new Dictionary<string, object>();
                finalResult.Add("friend_list", friends_result.dbReturningState);

                ISMBlacklistServiceProvider blacklist_provider = (ISMBlacklistServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.BLACKLIST];
                ISMServiceModel blacklist_model = new ISMServiceModel()
                {
                    operationIdentifier = ISMBlacklistServiceProvider.GET_BLACK_LIST,
                    userinfo = param
                };
                var blacklist_result = await blacklist_provider.doProcess(blacklist_model);
                if (blacklist_result.isOperationSuccess)
                    finalResult.Add("black_list", blacklist_result.dbReturningState);
                else
                    finalResult.Add("black_list", blacklist_result.errorMsg);
                response.userinfo = finalResult;
            }
            return response;
        }
        
        
        [HttpPost]
        public async Task<ZKResponse> AddFriend(ISMAddFriendRequest request)
        {
            ZKResponse response = new ZKResponse();
            if(request.friid == request.uid)
            {
                response.isProcessSuccess = false;
                response.errMsg = "不允许添加自己为好友";
                return response;
            }
            ISMFriendServiceProvider provider = (ISMFriendServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.FRIEND];
            ISMServiceModel model = new ISMServiceModel();
            model.operationIdentifier = ISMFriendServiceProvider.ADD_FRIEND;
            model.userinfo = request;
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }

        [HttpGet]
        public async Task<ZKResponse> CheckFriendStatus(UInt64 uid,UInt64 checkid,string token)
        {
            ZKResponse response = new ZKResponse();
            ISMFriendStatusCheckRequest request = new ISMFriendStatusCheckRequest()
            {
                checkid = checkid,
                uid = uid,
                token = token
            };
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMFriendServiceProvider.CHECK_FRIEND_STATUS,
                userinfo = request
            };
            ISMFriendServiceProvider provider = (ISMFriendServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.FRIEND];
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
    }
}
